package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Models.Equipment;
import Models.Movement;

public class MovementPresenter extends AbstractPresenter<Movement> {
	final String SINGLEITEM = "movement";
	final String MULTIITEM = "movements";
	
	public ArrayList<Movement> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public Movement tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public Movement createItemFromJSONObject(JSONObject jsonObject){
		Movement item = new Movement();
		try{
			item.setMovementBrewID(jsonObject.getInt("movementBrewID"));
			item.setMovementDate(timeFormat.parse(jsonObject.getString("movementDate")));
			item.setMovementEmployeeID(jsonObject.getInt("movementEmpID"));
			item.setMovementID(jsonObject.getInt("movementID"));
			item.setMovementNewState(jsonObject.getInt("movementStateID"));
			item.setMovementNewVesselID(jsonObject.getInt("movementNVID"));
			item.setMovementNotes(jsonObject.getString("movementNotes"));
			item.setMovementOldState(jsonObject.getInt("movementOldState"));
			item.setMovementOldVesselID(jsonObject.getInt("movementOVID"));
			item.setMovementVolume(jsonObject.getDouble("movementVolume"));
			
			//Set Documents Collection
			ArrayList<Equipment> equipment = new ArrayList<Equipment>();
			if(!jsonObject.isNull("equipmentCollection")){
				Object itm = jsonObject.get("equipmentCollection");
				EquipmentPresenter presenter = new EquipmentPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						equipment.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					equipment.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setEquipment(equipment);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(Movement item){
		try{
			JSONObject json = new JSONObject();
			json.put("movementBrewID", item.getMovementBrewID());
			json.put("movementDate", getDate(item.getMovementDate()));
			json.put("movementEmpID", item.getMovementEmployeeID());
			json.put("movementID", item.getMovementID());
			json.put("movementStateID", item.getMovementNewState());
			json.put("movementNVID", item.getMovementNewVesselID());
			json.put("movementNotes", item.getMovementNotes());
			json.put("movementOldState", item.getMovementOldState());
			json.put("movementOVID", item.getMovementOldVesselID());
			json.put("movementVolume", item.getMovementVolume());
			
			//Create Doc Collection
			if(item.getEquipment()!=null){
				EquipmentPresenter presenter = new EquipmentPresenter();
				if(item.getEquipment().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getEquipment().size(); i++){
						jsonArray.put(presenter.createJSON(item.getEquipment().get(i)));
					}
					json.put("equipmentCollection", jsonArray);
				}
			}
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Movement getEmptyEntity(){
		Movement entity = new Movement();
		return entity;
	}
}
